<?php
declare (strict_types = 1);

namespace app\admin\controller\setting;

use app\admin\controller\AuthController;
use app\admin\model\system\SystemConfig as ConfigModel;
use bbweb\services\CacheService;
use think\Request;

class SystemConfig extends AuthController
{

    /**
     * @param Request $req
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function basic(Request $req)
    {
        $list = ConfigModel::getAll('basic');
        if($req->isPost()) {
            $data = $req->param();
            foreach ($data as $k => $v) {
                $item = [
                    'menu_name' => $k,
                    'type'      => 'basic',
                    'value'     => $v['value'],
                    'parameter' => $v['parameter'],
                    'info'      => $v['info'],
                    'desc'      => $v['desc']
                ];
                if(array_key_exists($k, $list)) ConfigModel::edit($item, $k, 'menu_name');
                else ConfigModel::create($item);
            }
            CacheService::clear();
            $this->success();
        }
        $this->assign('list', $list);
        return $this->fetch();
    }
}
